<template>
  <div class="shopcart" v-show="totalCount > 0">
    <div class="content d-flex">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo">
            <img src="../assets/imgs/shopCart.svg" />
          </div>
          <!-- 商品总数量 -->
          <div class="num" v-show="totalCount > 0">
            {{ totalCount }}
          </div>
          <!-- 商品总价 -->
          <div class="price">￥{{ totalPrice }}元</div>
        </div>
      </div>

      <div class="pay" @click="toSettlement">
        {{ payDesc }}
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { Toast } from "vant";
import { setStorage } from "@/libs/utils";

export default {
  name: "ShopCart",
  computed: {
    ...mapState(["totalCount", "totalPrice", "selectedFood", "userId"]),
    // 配送价钱计算
    payDesc() {
      if (this.totalPrice === 0) {
        return `￥20元起送`;
      } else if (this.totalPrice < 20) {
        let diff = 20 - this.totalPrice;
        return `还差￥${diff}元起送`;
      } else {
        return `去结算`;
      }
    }
  },
  methods: {
    // 当商品总价大于 20 时，可点击去结算进行跳转
    toSettlement() {
      if (this.totalPrice >= 20) {
        // 暂存本地
        // sessionStorage.setItem(
        //   "selectedFood",
        //   JSON.stringify(this.selectedFood)
        // );

        // 测试
        console.log(this.selectedFood);
        // 添加购物车接口
        for (let item of this.selectedFood) {
          console.log(item.shopId);
          this.axios
            .post(
              "/api/shop/changeShopCart",
              `userId=${this.userId}&shopId=${item.shopId}&shopCount=${
                item.count
              }`
            )
            .then(result => {
              console.log(result);
              if (result.data.code == 200) {
                Toast("添加购物车成功");
                this.$router.push("/settlement");
              }
            })
            .catch(err => console.log(err));
        }
      }
    }
  }
  // updated() {
  //   setStorage({
  //     totalCount: this.totalCount,
  //     totalPrice: this.totalPrice,
  //     selectedFood: this.selectedFood
  //   });
  // }
};
</script>

<style lang="scss" scoped>
.shopcart {
  position: fixed;
  z-index: 50;
  bottom: 55px;
  left: 0;
  width: 100%;
  height: 48px;
  .content {
    position: relative;
    background-color: #e9e9e9;
    height: 100%;
    .content-left {
      position: absolute;
      width: 50%;
      .logo-wrapper {
        display: inline-block;
        position: absolute;
        top: -10px;
        margin: 0 12px;
        padding: 6px;
        width: 56px;
        height: 56px;
        box-sizing: border-box;
        vertical-align: top;
        border-radius: 50%;
        background-color: #fff;
        .logo {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          text-align: center;
          img {
            height: 34px;
            width: 34px;
            margin-top: 4px;
          }
        }
      }
      .num {
        position: absolute;
        top: 0;
        right: 0;
        width: 24px;
        height: 16px;
        line-height: 16px;
        text-align: center;
        border-radius: 16px;
        font-size: 9px;
        font-weight: 700;
        color: #fff;
        background: #fba92a;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.4);
      }
      .price {
        position: absolute;
        top: 21px;
        left: 70px;
        width: 90px;
        font-size: 16px;
        font-weight: 700;
        color: black;
      }
    }
    .pay {
      position: absolute;
      right: 0;
      box-sizing: border-box;
      height: 48px;
      line-height: 48px;
      background-color: rgb(219, 168, 113);
      padding: 0 30px;
      color: white;
    }
  }
}
</style>